package chenji.dailykit.ItemEditor.operationnode.nodes;

import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import chenji.dailykit.Main;
import chenji.dailykit.ItemEditor.ItemEditor;
import chenji.dailykit.ItemEditor.operationnode.OperationNode;

/**
 * @author ChenJi Copyright (c) 2016 ChenJi. All rights reserved.
 */
public class MenuNode extends OperationNode {
	/**
	 * state码 1 - 编辑标题模式； 0 - 正常模式；
	 */
	private int state;

	public MenuNode(ItemStack i, ItemEditor e, Player p) {
		super(i, e, p);
	}

	@Override
	public void setInventory(Inventory inv) {
		inv.setItem(28, Main.getOperationNodeItemLibrary().getChengeSubject());
		inv.setItem(29, Main.getOperationNodeItemLibrary().getChengeLore());
		inv.setItem(30, Main.getOperationNodeItemLibrary().getEnchantment());
	}

	@Override
	public void Click(int slot) {
		if (slot == 28) {
			this.getItemEditor().getChat();
			this.getPlayer().sendMessage("§a[ItemEditor] §c请输入标题");
			this.getPlayer().closeInventory();
			this.state = 1;
			return;
		}
		if (slot == 29) {
			this.getItemEditor().chengeLoreEditor();
			return;
		}
		if (slot == 30) {
			this.getItemEditor().chengeEnchantmentEditor();
			return;
		}
	}

	@Override
	public void upPage() {
	}

	@Override
	public void downPage() {
	}

	@Override
	public boolean setChat(String m) {
		if (this.state == 1) {
			ItemStack i = Main.getItemEditorManager().getItem(this.getPlayer());
			if (i == null) {
				Main.getItemEditorManager().removeEditor(this.getPlayer());
				this.getPlayer().sendMessage("§a[ItemEditor] §c错误，无法找到物品。请重试。");
				return true;
			}
			ItemMeta meta = i.getItemMeta();
			meta.setDisplayName(m);
			i.setItemMeta(meta);
			Main.getItemEditorManager().setItem(this.getPlayer(), i);
			Main.getItemEditorManager().openEditorInventory(this.getPlayer());
			this.getPlayer().sendMessage("§a[ItemEditor] 设置成功");
			return true;
		}
		return false;

	}

}
